package com.pzb.web.controller.page;

import com.pzb.page.domain.PgPage;
import com.pzb.page.service.IPgPageService;
import com.pzb.wechat.service.IWechatAppletService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 页面管理Controller
 * 
 * @author pzb
 * @date 2021-09-17
 */
@RestController
@RequestMapping("/page/page")
public class PgPageController extends BaseController
{
    @Autowired
    private IPgPageService pgPageService;
    @Autowired
    private IWechatAppletService wechatAppletService;

    /**
     * 查询页面管理列表
     */
    @PreAuthorize("@ss.hasPermi('page:page:list')")
    @GetMapping("/list")
    public TableDataInfo list(PgPage pgPage)
    {
        pgPage.setExtAppid(wechatAppletService.getExtAppid());
        startPage();
        List<PgPage> list = pgPageService.selectPgPageList(pgPage);
        return getDataTable(list);
    }

    /**
     * 查询页面管理列表
     */
    @PreAuthorize("@ss.hasPermi('page:page:list')")
    @GetMapping("/pageList")
    public AjaxResult pageList(PgPage pgPage)
    {
        pgPage.setExtAppid(wechatAppletService.getExtAppid());
        List<PgPage> list = pgPageService.selectPgPageList(pgPage);
        return AjaxResult.success(list);
    }

    /**
     * 获取页面管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('page:page:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pgPageService.selectPgPageById(id));
    }

    /**
     * 新增页面管理
     */
    @PreAuthorize("@ss.hasPermi('page:page:add')")
    @Log(title = "页面管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PgPage pgPage)
    {
        String extAppid = wechatAppletService.getExtAppid();
        if("1".equals(extAppid)){
            return AjaxResult.error("您还没有小程序!");
        }
        pgPage.setExtAppid(extAppid);
        return toAjax(pgPageService.insertPgPage(pgPage));
    }

    /**
     * 修改页面管理
     */
    @PreAuthorize("@ss.hasPermi('page:page:edit')")
    @Log(title = "页面管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PgPage pgPage)
    {
        return toAjax(pgPageService.updatePgPage(pgPage));
    }

    /**
     * 删除页面管理
     */
    @PreAuthorize("@ss.hasPermi('page:page:remove')")
    @Log(title = "页面管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(pgPageService.deletePgPageById(id));
    }


    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('page:page:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changePageState")
    public AjaxResult changePageState(@RequestBody PgPage page)
    {
        page.setUpdateBy(getUsername());
        return toAjax(pgPageService.updatePgPage(page));
    }


    /**
     * 主页修改
     */
    @PreAuthorize("@ss.hasPermi('page:page:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changePageHome")
    public AjaxResult changePageHome(@RequestBody PgPage page)
    {
        page.setUpdateBy(getUsername());
        page.setExtAppid(wechatAppletService.getExtAppid());
        return toAjax(pgPageService.changePageHome(page));
    }


}
